#include "Octopi.h"

#include <iostream>

Octopi::Octopi() {
}
Octopi::~Octopi() {
}

void Octopi::Initialize(sf::RenderWindow* window, sf::Texture* texture, sf::Texture* attackTexture, sf::Texture* splashTexture, sf::Texture* deadTexture, sf::Texture* takeDamageTexture, bool in, int type, sf::Texture* bloodTexture) {
 m_renderWindow = window;
 
 m_alphaBlood = 200.f;

 m_friend = in;

 m_type = type;

 m_deadTexture = deadTexture;
 m_attackTexture = attackTexture; 

 m_bloodSprite = new sf::Sprite;
 m_bloodSprite->setTexture(*bloodTexture);

 m_bloodAni = new Animation;
 m_bloodAni->Initialize(bloodTexture, sf::Vector2f(500,500), 6, 24, 0);

 m_shadowAni = new Animation;
 m_shadowAni->Initialize(texture, sf::Vector2f(500,500), 3, 20, 5);

 m_octSprite = new sf::Sprite();
 m_octSprite->setTexture(*texture);

 m_hitSprite = new sf::Sprite();
 m_hitSprite->setTexture(*splashTexture);

 m_takeDamageSprite = new sf::Sprite();
 m_takeDamageSprite->setTexture(*takeDamageTexture);

 m_splashAni = new Animation;
 m_splashAni->Initialize(splashTexture, sf::Vector2f(500,500), 6,24,0);

 m_anim = new Animation;
 if(m_type == 1){
	m_anim->Initialize(attackTexture, sf::Vector2f(500,500),11,30,1);
 }else if(m_type == 2){
	 m_anim->Initialize(attackTexture, sf::Vector2f(500,500),4,24,3);
 }else if(m_type == 3){
	m_anim->Initialize(attackTexture, sf::Vector2f(500,500), 4,30,2);
 }

 m_bloodPos = false;
 m_bloodTimer = 0.f;

 m_frames = 0;

 m_clock = new sf::Clock();
 m_attackClock = new sf::Clock();
 m_hitSprite->scale(3.f, 3.f);
 
 m_scale = 0.05f;
 m_octSprite->setScale(m_scale, m_scale);

 m_hitSprite = 0;
 m_attack = false;
 m_hitTime = 0.f;
 //m_posY = 300.f;
 m_posY = 250.f;
 m_alpha = 10;
 m_attackTimer = 0.f;
 m_hit = false;

// std::cout << m_posX << std::endl;
 alreadyShownScore = false;
 m_canDie = false;
 m_isDead = false;

 m_octSprite->setPosition(m_posX,m_posY);
 m_octSprite->setScale(m_scale, m_scale);

 m_isFriendly = false;
 m_isFirst = false;
}

void Octopi::Update(Player*	player, TextManager* textManager) {
	  m_deltaTime = m_clock->restart().asSeconds();
	  m_attackTimer += m_deltaTime;

	  if(m_attackTimer > 2.0f  && m_hit == false){
		  m_octSprite = m_anim->GetSprite();
		  m_anim->Update(m_deltaTime);
		  m_octSprite->setScale(m_scale, m_scale);
		}

	m_octSprite->setOrigin(m_octSprite->getGlobalBounds().width/2,m_octSprite->getGlobalBounds().height/2);
	  /*
	  if(m_octSprite->getScale().x < 1.0f){
		  if(m_octSprite->getColor().Transparent <;
		  m_octSprite->setColor(sf::Color::Magenta);
		m_octSprite->setScale(m_scale, m_scale);
		m_scale += 0.01f;
	  }
	  */

	  if(m_attackTimer < 2.0f && m_hit == false){
		  m_shadowAni->Update(m_deltaTime);
		  m_octSprite = m_shadowAni->GetSprite();
		  m_posY += 30 * m_deltaTime;
		  m_octSprite->setPosition(m_posX,m_posY);
		  m_scale += 0.2f * m_deltaTime;
		  m_alpha += 80 * m_deltaTime;
		  m_octSprite->setColor(sf::Color(m_octSprite->getColor().r, m_octSprite->getColor().g, m_octSprite->getColor().b, m_alpha));
		//  if(m_octSprite->getScale().x < 0.7f){
			m_octSprite->setScale(m_scale, m_scale);
		//  }

			// ATTTATATATATCKCKAKCKA!
	  }else if(m_attackTimer < 3.0f && m_hit == false){
		  m_canDie = true;
		  m_posY += 250 * m_deltaTime;
		  m_octSprite->setPosition(m_posX,m_posY);
		  m_octSprite->setTexture(*m_attackTexture);


		  m_octSprite->setColor(sf::Color(m_octSprite->getColor().r, m_octSprite->getColor().g, m_octSprite->getColor().b, 255));
		  if(m_octSprite->getScale().x < 0.8f){
			m_scale += 1.1f * m_deltaTime;
			m_octSprite->setScale(m_scale, m_scale);
		  }

	  }else if(m_attackTimer > 3.0f){
		  m_posY += 400 * m_deltaTime;
		  
		  if(m_octSprite->getScale().x < 1.1f){
			m_scale += 0.4f * m_deltaTime;
			m_octSprite->setScale(m_scale, m_scale);
		  }
		  m_octSprite->setPosition(m_posX,m_posY);
		  if(m_octSprite->getPosition().y - m_octSprite->getGlobalBounds().height > 720){
			m_isDead = true;			
		  }
	  }
	  

	  // DIES
	  if(m_hit == true){
		  m_bloodAni->Update(m_deltaTime); 
		  m_bloodSprite = m_bloodAni->GetSprite();
		//  m_anim->Reset();
	//	  m_octSprite->setTexture(*m_deadTexture);
		m_attackTimer = 0.f;
		m_scale -= 1.5f * m_deltaTime;
		if(side == 0){
			m_posY -= 2000 * m_deltaTime;
			m_posX += 1000 * m_deltaTime;
		}else if (side == 1){
			m_posY -= 2000 * m_deltaTime;
			m_posX -= 1000 * m_deltaTime;
		}
		m_octSprite->rotate(10);
		if(alreadyShownScore == false) {
			if (m_friend == true) {
				textManager->addFriendlyScore(m_renderWindow);
				textManager->m_arrayOfFriendlyScore[textManager->m_arrayOfFriendlyScore.size()-1]->setPositionFriendlyScore(m_octSprite->getPosition().x, m_octSprite->getPosition().y);

			} else {
				textManager->addKillScore(m_renderWindow);
				textManager->m_arrayOfKillScore[textManager->m_arrayOfKillScore.size()-1]->setPositionKillScore(m_octSprite->getPosition().x, m_octSprite->getPosition().y);
			}
			alreadyShownScore = true;
		}
		if(m_octSprite->getScale().x > 0.01f){
			m_octSprite->setScale(m_scale, m_scale);
		}
		m_octSprite->setPosition(m_posX,m_posY); 
		if(m_posY < 0){
			m_isDead = true;
			if(m_friend == false){
				player->addScore(500);
			}else if(m_friend == true){
				player->addScore(-1000);
			}
		}
	  }


	  m_renderWindow->draw(*m_octSprite);

	  if(m_attackTimer > 1.9f && m_attackTimer < 2.15f){
		  m_hitSprite = m_splashAni->GetSprite();
		  m_splashAni->Update(m_deltaTime);
	//	  m_hitSprite->setOrigin(m_hitSprite->getGlobalBounds().width/2,m_hitSprite->getGlobalBounds().height/2);
		  if(m_frames == 0){
				m_hitSprite->setPosition(m_octSprite->getGlobalBounds().left + 120.f, m_octSprite->getGlobalBounds().top + 120.f);
				m_frames = 1; 
		  }
		  m_renderWindow->draw(*m_hitSprite);
	  }
	  /*
	  if(m_attack == true && m_attackTimer < 2.5f){
		  m_takeDamageSprite->setPosition(m_octSprite->getGlobalBounds().left + 120.f, m_octSprite->getGlobalBounds().top + 120.f);
		  m_takeDamageSprite = m_takeDamageAni->GetSprite();
		  m_splashAni->Update(m_deltaTime);
		  m_renderWindow->draw(*m_takeDamageSprite);
	  }*/

	  if(m_octSprite->getScale().x > 0.99f && m_hitTime < 0.2f){
	//	  m_hitSprite->setPosition(m_octSprite->getPosition().x - 80.f, m_octSprite->getPosition().y - 50.f);
		//  m_renderWindow->draw(*m_hitSprite);
		  m_hitTime += m_deltaTime;
		  if(m_attack == false){
			  if(m_friend == false){
					m_canDie = false;
					player->takeDamage(10);
					m_attack = true;
			  }else if(m_friend == true){
				//	m_attack = true;
					m_canDie = false;
			  }
				
		  }
	 }

	  if(m_attack == true){
		  m_alphaBlood -= 120*m_deltaTime;
		  if(side == 0 && m_takeDamageSprite->getScale().x != -1.f){
				m_takeDamageSprite->scale(-1.f, 1.f);
		  }else if(side == 1){
		  }

		  if(side == 0){
				m_takeDamageSprite->setPosition(player->getSprite()->getPosition().x + 1000.f, 0.f);
		   }else if(side == 1){
		  		  m_takeDamageSprite->setPosition(player->getSprite()->getPosition().x - 1000.f, 0.f);
		  }

		  m_takeDamageSprite->setColor(sf::Color(m_octSprite->getColor().r, m_octSprite->getColor().g, m_octSprite->getColor().b, m_alphaBlood));
		  m_renderWindow->draw(*m_takeDamageSprite);
	  }

	  // DIES
	  if(m_hit == true && m_bloodTimer < 0.08f){
		   m_bloodTimer += m_deltaTime;
		  if(m_bloodPos == false){
			  m_bloodSprite->setPosition(m_octSprite->getPosition().x, m_octSprite->getPosition().y);
			 m_bloodPos = true;
		  }
		  m_bloodAni->Update(m_deltaTime); 
		  m_bloodSprite = m_bloodAni->GetSprite();
		  m_renderWindow->draw(*m_bloodSprite);
	  }

}

void Octopi::Cleanup() {
	delete m_octSprite;
	delete m_hitSprite;
	delete m_anim;
	delete m_splashAni;
	delete m_clock;
	delete m_attackClock;
	delete m_bloodAni;
	delete m_bloodSprite;
}